import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import java.util.List;

public class XpathPlan {
    public void xpath(String target){
        String file = "d:/java/xml/src/plan.xml";
        SAXReader reader = new SAXReader();
        try {
            Document document = reader.read(file);
            List<Node> nodes = document.selectNodes(target);
            for (Node node:nodes) {
                Element course = (Element)node;
                System.out.println("课程编号："+course.attributeValue("no"));
                System.out.println("课程名称："+course.elementText("course_name"));
                System.out.println("课时："+course.elementText("class_hour"));
                System.out.println("考核方式："+course.elementText("exam_form"));
                System.out.println("===============================");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        XpathPlan xpathPlan = new XpathPlan();
        // 查询课时小于50的course
        xpathPlan.xpath("//course[class_hour<50]");
        // 课程名称是高等数学的course
        xpathPlan.xpath("//course[course_name='高等数学']");
        // 课程编号为1的course
        xpathPlan.xpath("//course[no = '1']");
        // 前两个course的信息
        xpathPlan.xpath("//course[position()<2]");

    }
}
